package edu.yuhf.sql;

import edu.yuhf.vo.QueryParameter;

import java.util.Optional;

public class BuilderSQL {

    public static Optional<String> builderQuery(final Optional<QueryParameter> optional){
        StringBuilder[] sb = {new StringBuilder()};
        optional.ifPresentOrElse(qp->{
            sb[0] = new StringBuilder("Select * from ");
            sb[0].append(qp.getTableName());
            sb[0].append(" where 1=1 ");
            //todo:这里需要用循环改造
            if(0!=qp.getId()){
                sb[0].append("and id=? ");
            }
            System.out.println(qp.getName());
            if(null!=qp.getName()&&!qp.getName().equals("")){
                sb[0].append("and name=?");
            }
            if(null!=qp.getSex()&&!qp.getSex().equals("")){
                sb[0].append("and sex=?");
            }
        },()->{
            System.out.println("传入的值无效。");
        });
        QueryParameter qp = optional.orElse(new QueryParameter());
        return Optional.ofNullable(sb[0].toString());
    }
}
